<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:include="common/head :: htmlhead">
<body>
<script th:src="@{/static/plugins/ueditor/ueditor.config.js}"></script>
<script th:src="@{/static/plugins/ueditor/ueditor.all.min.js}"></script>
<script th:src="@{/static/plugins/ueditor/lang/zh-cn/zh-cn.js}"></script>
<style>
    .layui-form-select dl {
        z-index: 9999999999999999999999;
    }
</style>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">内容添加</div>
                <div class="layui-card-body">
                    <form class="layui-form" id="form1" action="" lay-filter="component-form-element">
                        <div class="layui-row layui-col-space10 layui-form-item">
                            <div class="layui-col-lg12">
                                <label class="layui-form-label">文章标题</label>
                                <div class="layui-input-block">
                                    <input id="title" type="text" name="title" lay-verify="required"
                                           lay-reqtext="标题不能为空！"
                                           placeholder="请输入标题" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                        </div>
                        <div class="layui-row layui-col-space10 layui-form-item">
                            <div class="layui-col-lg12">
                                <label class="layui-form-label">所属类别</label>
                                <div class="layui-input-block">
                                    <select id="type" name="type">
                                        <option value=""></option>
                                        <option value="1">新闻动态</option>
                                        <option value="2">活动展讯</option>
                                        <option value="3">通知通告</option>
                                        <option value="4">学术成果</option>
                                    </select>
                                </div>
                            </div>
                        </div>
                        <div class="layui-row layui-col-space10 layui-form-item">
                            <div class="layui-col-lg12">
                                <label class="layui-form-label">文章内容</label>
                                <div class="layui-input-block">
                                    <div>
                                        <script id="editor" type="text/plain" style="width:100%;height:500px;"></script>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-input-block">
                                <button type="button" class="layui-btn" onclick="tijiao('dsh')">提交</button>
                                <button type="button" class="layui-btn layui-btn-primary" onclick="tijiao('cgx')">存为草稿
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<script type="text/javascript">
    //实例化编辑器
    // 建议使用工厂方法getEditor创建和引用编辑器实例，如果在某个闭包下引用该编辑器，直接调用UE.getEditor('editor')就能拿到相关的实例
    var ue = UE.getEditor('editor');

    function tijiao(aa) {
        var context = UE.getEditor('editor').getContent();
        var title = document.getElementById("title").value;
        var type = document.getElementById("type").value;
        var datas = {};
        datas["context"] = context;
        datas["title"] = title;
        datas["typeid"] = type;
        datas["status"] = aa;
        if (title === "") {
            layer.msg('文章标题不能为空！', {icon: 2});
            return false;
        }
        if (type === "") {
            layer.msg('请选择新闻类型！', {icon: 2});
            return false;
        }
        layer.confirm('确定提交？', function (index) {
            layer.close(index);
            $.ajax({
                url: "/addcontent",
                async: false,
                type: "POST",
                contentType: "application/json",
                data: JSON.stringify(datas),
                success: function (data) {
                    if (data === "success") {
                        layer.open({
                            title: '信息'
                            , content: '提交成功！'
                            , end: function () {
                                location.reload();
                            }
                        });
                    }else{
                        layer.msg('操作失败！', {icon: 2});
                    }
                }
            });
        })
    }

    layui.use('form', function () {
        var form = layui.form;
        form.render(null, 'component-form-element');
    });
</script>
</body>
</html>